// components/Player/RecentMatches/RecentMatches.js
const app = getApp()
import http from '../../../utils/request.js'
import api from '../../../utils/api.js'
Component({
  /**
   * 组件的属性列表
   */
  options: {
    styleIsolation: 'apply-shared'
  },
  properties: {
    player: Object,
    totals: Object,
  },
  /**
   * 组件的初始数据
   */
  data: {
    downList: false,
    recentMatches: [],
    heroes:[],
    page: 1,
    heroPage: 1
  },
  lifetimes: {
    attached() {
      
    },
    detached() {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    clk() {
      this.setData({
        downList: !this.data.downList
      })
    },
    openMatch(e){
      app.gotoMatch(e.currentTarget.dataset.matchid)
    },
    more() {
      this.setData({ page: this.data.page + 1 })
      this.loadMatches()
    },
    moreHero() {
      this.setData({ heroPage: this.data.heroPage + 1 })
      this.loadHeroes()
    },
    loadMatches() {
      http.get(api.playerRecentMatchs, {
        id: this.data.player.id,
        size: 5,
        current: this.data.page
      })
        .then(result => {
          let list = result.data.data.records;
          list.map(s => {
            s.endTime = new Date(s.endTime).getTime() / 1000
          })
          list = this.data.recentMatches.concat(list)
          this.setData({
            recentMatches: list
          })
        });
    },
    loadHeroes() {
      http.get(api.playerHeroes, {
        playerId: this.data.player.id,
        size: 5,
        current: this.data.heroPage
      })
        .then(result => {
          let list = result.data.data.records;
          list = this.data.heroes.concat(list)
          this.setData({
            heroes: list
          })
        });
    }
  },
  observers: {
    player() {
      if (this.data.player) {
        this.loadMatches()
        this.loadHeroes()
      }
    }
  }
})